﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Drawing;
using System.Windows.Forms;
using Duoease.ContainerModel.Utils;
using System.Drawing.Drawing2D;
using SmartMD.DataModel;

namespace Duoease.ContainerModel.Entity
{
    public class BoxModel : BaseModel
    {
        /// <summary>
        /// 单选或多选容器元素
        /// </summary>     
        private bool _isSingleSelected = true; //单选标识
        private List<string> _selectedItemIdList = new List<string>();  //选择的元素标识集合
        public List<string> SelectedItemIdList
        {
            get { return _selectedItemIdList; }
        }

        private List<PointF[]> _selectedPfList = new List<PointF[]>(); //选择的面集合

        #region 构造函数
        public BoxModel()
        {
            this.Title = "盒子";
            this.TagName = "BoxModel";
        }
        public BoxModel(string tile)
        {
            this.Title = tile;
            this.TagName = "BoxModel";
        }
        public BoxModel(int row, int column, string tile)
        {
            Row = row;
            Column = column;
            this.Title = tile;
            this.TagName = "BoxModel";
        }
        public BoxModel(int row, int column, Color surfaceColor, int alpha, string tile)
        {
            Row = row;
            Column = column;
            SurfaceColor = surfaceColor;
            Alpha = alpha;
            this.Title = tile;
            this.TagName = "BoxModel";
        }
        public BoxModel(int row, int column, Color surfaceColor, int alpha, Color lineColor, int lineWidth, string tile)
        {
            Row = row;
            Column = column;
            SurfaceColor = surfaceColor;
            Alpha = alpha;
            LineColor = lineColor;
            LineWidth = lineWidth;
            this.Title = tile;
            this.TagName = "BoxModel";
        }
        #endregion

        /**
        * 功能：解析模型文件
        * 
        * */
        public override void ParseXml(XmlNode xn)
        {
            if (xn == null)
            {
                return;
            }
            base.ParseXml(xn);
        }
        /**
         * 功能：刷新控件
         * 
         * */
        public override void Refresh()
        {
            this.View.rightlabel.Text = this.Name + this.View.FridgeModel.Name + ID;

            /*if (this.View.IsCreatNewModel)
            {
                this.View.docmodel.ConstructXml(this, ModelStyle.BoxModel, this.View.ctnNo);
            }*/

            this.IsFull = new bool[Row, Column]; //盒子标本存入状态
    
            this.View.BoxWidth = this.panel.Width;
            this.View.BoxHeight = this.panel.Height;
            ContainerWidth = this.View.BoxWidth;
            ContainerHeight = this.View.BoxHeight;

            this.panel.Refresh();
        
            AffixToLabel(this.View.rightlabel, this.View.BoxHeight, this.View.BoxHeight);
        }

        /**
        * 功能：构造模型文件
        * 
        * */
        public override XmlElement ConstructXml(XmlDocument doc, string tagName, XmlElement element = null)
        {
            return base.ConstructXml(doc, tagName, element);
        }
        /**
         * 功能 ：绘制函数
         * 
         * */
        public override void Draw()
        {
            if (Row < 1 || Column < 1)
            {
                return;
            }
            g.Clear(BackGround);
            this.Location.X = ContainerWidth / (Column + 2);
            this.Location.Y = ContainerHeight * 3 / 5;
            JoinBoxModel(g, Location, Row, Column, UnitWidth, UnitHeight, Alpha, SurfaceColor, LineColor, LineWidth);
        }

        public override void AffixToLabel(Label oldLabel, int width, int height)
        {
            Label newLabel = new Label();

            this.panel.Controls.Clear();
            this.panel.Controls.Add(oldLabel);
            //this.BoxModel.ContainerWidth = width;
            //this.BoxModel.ContainerHeight = height;

            int k = 1;
            for (int i = Row - 1; i >= 0; i--)
            {
                newLabel = new Label();
                newLabel.Width = 20;
                newLabel.Text = ModelUtil.GetLetter(k);
                newLabel.Font = new Font(newLabel.Font.FontFamily, 15, FontStyle.Bold);
                newLabel.ForeColor = Color.Black;
                newLabel.BackColor = Color.Transparent;

                newLabel.MouseDown += new MouseEventHandler(this.View.BnewLabel_MouseDown);

                newLabel.Location = new Point(Location.X + Slope * i - 15, Location.Y - Slope * i - UnitHeight / 2 - 10);

                this.panel.Controls.Add(newLabel);
                k++;
            }
            for (int j = 0; j < Column; j++)
            {
                newLabel = new Label();
                newLabel.Width = 20;
                newLabel.Text = (j + 1).ToString();
                newLabel.Font = new Font(newLabel.Font.FontFamily, 15, FontStyle.Bold);
                newLabel.ForeColor = Color.Black;
                newLabel.BackColor = Color.Transparent;

                newLabel.Location = new Point(Location.X + Slope * (Row - 1) + UnitWidth * j + UnitWidth / 2 + 5, Location.Y - Slope * (Row + 1) - 2);

                newLabel.MouseDown += new MouseEventHandler(this.View.BnewLabel_MouseDown);
                this.panel.Controls.Add(newLabel);
            }
        }

        /**
        * 功能：鼠标单击事件
        * 
        * */
        public override void OnMouseOverHandler(object sender, MouseEventArgs e)
        {
            Point p = new Point(e.X, e.Y);
            int x = 0;
            int y = 0;
            Point origin = new Point(Location.X + (Row - 1) * Slope, Location.Y - (Row - 1) * Slope);
            g = this.panel.CreateGraphics();

            PointF[] pf = new PointF[4];
            GraphicsPath gp = new GraphicsPath();
            Point p1 = Location;
            Point p2 = new Point(p1.X + UnitWidth * Column, p1.Y);
            Point p3 = new Point(p1.X + Slope * Row, p1.Y - Slope * Row);
            Point p4 = new Point(p3.X + UnitWidth * Row, p3.Y);
            pf[0] = p4;
            pf[1] = p3;
            pf[2] = p1;
            pf[3] = p2;
            gp.AddLines(pf);
            if (gp.IsVisible(p))
            {
                for (int i = 0; i < Row; i++)
                {
                    for (int j = 0; j < Column; j++)
                    {
                        gp = new GraphicsPath();
                        p1 = new Point(origin.X + j * UnitWidth - i * Slope, origin.Y + i * Slope);
                        p2 = new Point(p1.X + UnitWidth, p1.Y);
                        p3 = new Point(p1.X + Slope, p1.Y - Slope);
                        p4 = new Point(p3.X + UnitWidth, p3.Y);
                        pf[0] = p4;
                        pf[1] = p3;
                        pf[2] = p1;
                        pf[3] = p2;
                        gp.AddLines(pf);

                        if (gp.IsVisible(p))
                        {
                            //g.FillPath(new SolidBrush(Color.FromArgb(100, Color.Red)), gp);
                            x = i + 1;
                            y = j + 1;
                            break;
                        }
                    }
                }
                if (this.tooltips != null)
                {
                    this.View.mouseTip.SetToolTip(this.panel.Controls.Owner, this.tooltips[x, y]);
                }

            }
            else
            {
                this.View.mouseTip.RemoveAll();
            }
        }

        /**
         * 功能：cell多选功能
         * 
         * */
        public event MouseEventHandler CellMultiSelectedEvent;
        public void OnMultiSelectedHandler(object sender, MouseEventArgs e)
        {
            Point p = new Point(e.X, e.Y);
            int x = 0;
            int y = 0;
            Point origin = new Point(Location.X + (Row - 1) * Slope, Location.Y - (Row - 1) * Slope);

            PointF[] pf = new PointF[4];
            GraphicsPath gp = new GraphicsPath();
            Point p1 = Location;
            Point p2 = new Point(p1.X + UnitWidth * Column, p1.Y);
            Point p3 = new Point(p1.X + Slope * Row, p1.Y - Slope * Row);
            Point p4 = new Point(p3.X + UnitWidth * Row, p3.Y);
            pf[0] = p4;
            pf[1] = p3;
            pf[2] = p1;
            pf[3] = p2;
            gp.AddLines(pf);
            if (gp.IsVisible(p))
            {
                for (int i = 0; i < Row; i++)
                {
                    for (int j = 0; j < Column; j++)
                    {
                        gp = new GraphicsPath();
                        p1 = new Point(origin.X + j * UnitWidth - i * Slope, origin.Y + i * Slope);
                        p2 = new Point(p1.X + UnitWidth, p1.Y);
                        p3 = new Point(p1.X + Slope, p1.Y - Slope);
                        p4 = new Point(p3.X + UnitWidth, p3.Y);
                        pf[0] = p4;
                        pf[1] = p3;
                        pf[2] = p1;
                        pf[3] = p2;
                        gp.AddLines(pf);

                        if (gp.IsVisible(p))
                        {
                            //绘制选择元素
                            //this.g = this.View.g;

                            x = i + 1;
                            y = j + 1;
                            this.View.MouseRow = x;
                            this.View.MouseColumn = y;
                            this.View.SelectedItem = this;
                            this.View.SelectedItemID = ID + "-" + ModelUtil.GetLetter(x).ToString() + y.ToString();
                            if (!_isSingleSelected) //多选
                            {

                                int index = _selectedItemIdList.IndexOf(this.View.SelectedItemID);
                                if (index < 0)
                                {
                                    _selectedItemIdList.Add(this.View.SelectedItemID);
                                    _selectedPfList.Add(pf);

                                }
                                /* else
                                 {
                                     _selectedItemIdList.RemoveAt(index);
                                     _selectedPfList.RemoveAt(index);
                                 }*/
                                for (int k = 0; k < _selectedPfList.Count; k++)
                                {
                                    GraphicsPath gp2 = new GraphicsPath();
                                    gp2.AddLines(_selectedPfList[k]);
                                    g.FillPath(new SolidBrush(Color.FromArgb(255, Color.Blue)), gp2);

                                }

                            }
                            else //单选
                            {
                                this.Draw();
                                g.FillPath(new SolidBrush(Color.FromArgb(255, Color.Blue)), gp);
                            }
                            if (this.CellMultiSelectedEvent != null)
                            {
                                this.CellMultiSelectedEvent(sender, e);
                            }
                            break;
                        }
                    }
                }
            }
        }
    }
}
